var log4js       = require("log4js");
var log4jsconfig = require("../config/log4js");
var config       = require('../config/config');
//var log4jsextend = require("./log4jsextend"); //增加此文件，则不必依赖修改log4js,但是idc机器上的log4js已经是修改了的，感觉可以不用此文件
var env          = require("./env");
var user         = {};
var ip           = env.getIp();


var logip   = config._config.logip;
var logPort = config._config.logPort;


var masterAppender = {
    "type"    : "logLevelFilter",
    "level"   : "debug",
    "appender": {
        "type"      : global.prefixPath + "/src/utils/multiserverappender",
        "mode"      : "master",
        "loggerPort": logPort,
        "loggerHost": logip,
        "appender"  : {
            "type"                : 'dateFile',
            "filename"            : 'log/all-log.log',
            "pattern"             : ".yyyy-MM-dd",
            "maxLogSize"          : 1024,
            "alwaysIncludePattern": true,
            "backups"             : 4
        }
    }
}

var workerAppender = {
    "type"    : "logLevelFilter",
    "level"   : "DEBUG",
    "appender": {
        "type"      : global.prefixPath + "/src/utils/multiserverappender",
        "mode"      : "worker",
        "loggerPort": logPort,
        "loggerHost": logip
    },
}

/**
 * 日志配置
 */
exports.configure = function () {

    if (env.getEnv() == 'local' || env.getEnv() == 'dev' || env.getEnv() == 'gamma' || env.getEnv() == 'idc') {
      // log4js.configure({
      //   appenders: {
      //       "info": {
      //         "type": "dateFile",
      //         "filename": "./logs/info-",
      //         "encoding": "utf-8",
      //         "pattern": ".yyyy-MM-dd.log",
      //         "maxLogSize": 10000000,
      //         "alwaysIncludePattern": true,
      //         "layout": {
      //           "type": "pattern",
      //           "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
      //         },
      //         "compress": true
      //       },
      //       "maxInfo": {
      //         "type": "logLevelFilter",
      //         "appender": "info",
      //         "level": "debug",
      //         "maxLevel": "error"
      //       },
      //       "error": {
      //         "type": "dateFile",
      //         "filename": "./logs/error-",
      //         "pattern": ".yyyy-MM-dd.log",
      //         "maxLogSize": 10000000,
      //         "encoding": "utf-8",
      //         "alwaysIncludePattern": true,
      //         "layout": {
      //           "type": "pattern",
      //           "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
      //         },
      //         "compress": true
      //       }
      //   },
      //   categories: { default: { appenders: ['info', 'maxInfo', "error"], level: 'error' } },
      //   pm2: true,
      //   replaceConsole: true
      // });

      log4js.configure({
        "appenders": {
          "access": {
            "type": "dateFile",
            "filename": "./logs/access-",
            "encoding": "utf-8",
            "pattern": ".yyyy-MM-dd.log",
            "maxLogSize": 10000000,
            "alwaysIncludePattern": true,
            "layout": {
              "type": "pattern",
              "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
            },
            "compress": true,
            "category": "http"
          },
          "app": {
            "type": "dateFile",
            "filename": "./logs/app-",
            "encoding": "utf-8",
            "pattern": ".yyyy-MM-dd.log",
            "maxLogSize": 10000000,
            "alwaysIncludePattern": true,
            "layout": {
              "type": "pattern",
              "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
            },
            "compress": true
          },
          "errorFile": {
            "type": "dateFile",
            "filename": "./logs/error-",
            "encoding": "utf-8",
            "pattern": ".yyyy-MM-dd.log",
            "maxLogSize": 10000000,
            "alwaysIncludePattern": true,
            "layout": {
              "type": "pattern",
              "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
            },
            "compress": true
          },
          "errors": {
            "type": "logLevelFilter",
            "level": "ERROR",
            "appender": "errorFile"
          }
        },
        "categories": {
          "default": { "appenders": [ "app", "errors" ], "level": "DEBUG" },
          "http": { "appenders": [ "access"], "level": "DEBUG" }
        },
        pm2: true,
        replaceConsole: true
      });
      
    } else if (ip == logip) {

        log4jsconfig._master_config.appenders.push(masterAppender);
        log4js.configure(log4jsconfig._master_config);
    } else {

        log4jsconfig._worker_config.appenders.push(workerAppender);
        log4js.configure(log4jsconfig._worker_config);
    }
    //log4jsextend.extend(log4js); //因为灰度和idc机器已经改了log4js，不采用此方式了
}

/**
 * 暴露到应用的日志接口，调用该方法前必须确保已经configure过
 * @param name 指定log4js配置文件中的category。依此找到对应的appender。
 *              如果appender没有写上category，则为默认的category。可以有多个
 * @returns {Logger}
 */
exports.logger = function (name) {
    //获取erpname
    // console.log("start logining : " + user.name + " " + name);
    var dateFileLog = log4js.getLogger(user.name ? "[" + user.name + "]" + "[" + ip + "] " + name : "[" + ip + "] " + name);
    // console.log(dateFileLog.log.toString());
    return dateFileLog;
}

/**
 * 设置用户信息
 * @author huangshaolu
 * @date   2016-03-28
 * @param  {String}   name 用户名
 */
exports.setUserName = function (name) {

    log4js.setLoggerPrefix(name); //服务器上的log4js已经有此方法了，暂时不用log4jsextend
    //log4jsextend.setLoggerPrefix(name);
}

